﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Ablactationss
{
    public class DeleteAblactationsHander : IRequestHandler<DeleteAblactationCommand, Result>
    {
        private readonly IBaseRepository<Ablactations> ablactationsRepository;

        public DeleteAblactationsHander(IBaseRepository<Ablactations> ablactationsRepository)
        {
            this.ablactationsRepository = ablactationsRepository;
        }

        public async Task<Result> Handle(DeleteAblactationCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var list = await (await ablactationsRepository.GetAll()).Where(x=>x.IsDels==false).FirstAsync();
                if (list != null)
                {
                    list.IsDels = true;
                }
                var res = await ablactationsRepository.Update(list);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "母猪断奶删除成功";
                }
            }
            catch (Exception)
            {
                result.Msg = "母猪断奶删除异常";
            }
            return result;
        }
    }
}
